﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class MahalleYaz
    {
        public byte[] Yaz(List<MahalleBilgi> mahalleBilgiList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "Mahalle Listesi");

            int satir = 0;
            int sutun = 0;

            workBook.setText(satir, sutun, "Mahalle ID");
            sutun++;
            workBook.setText(satir, sutun, "Adı");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            //workBook.setText(satir, sutun, "Kurum ID");
            //sutun++;

            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (MahalleBilgi mahalleBilgi in mahalleBilgiList)
            {
                satir++;
                sutun = 0;

                if (mahalleBilgi.MahalleBilgiID != null)
                    deger = mahalleBilgi.MahalleBilgiID.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (mahalleBilgi.Ad != null)
                    deger = mahalleBilgi.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;
                if (mahalleBilgi.Aktifmi != null)
                    deger = mahalleBilgi.Aktifmi.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                //if (mahalleBilgi.KurumID != null)
                //    deger = mahalleBilgi.KurumID.ToString();
                //workBook.setText(0, satir, sutun, deger);
                //deger = string.Empty;
                //sutun++;

                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun - 1);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}

